﻿using System;
using System.IO;
using System.Collections;

namespace BoxDropbox
{
    class DirectoryFetcher
    {

        #region Singleton Info
        private static DirectoryFetcher instance;

        private DirectoryFetcher() {}

        public static DirectoryFetcher Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DirectoryFetcher();
                }

                return instance;
            }
        }
        #endregion

        private string _root;
       // private ArrayList _folderList;
        private ArrayList _fileList;

        public string root
        {
            set
            {
                _root = value;
            }
        }

        /// <summary>
        /// Debug helper function to print an ArrayList - will only print with DEBUG_LOG
        /// </summary>
        /// <param name="al">Array list to print</param>
        /// <param name="msg">Message to print first.</param>
        private void printArrayList(ArrayList al, string msg)
        {
#if DEBUG_LOG
            Console.WriteLine(msg);
            foreach (string el in al)
            {
                Console.WriteLine(el);
            }
#endif
        }

        /// <summary>
        /// Currently prints list of all local files in the set path.
        /// </summary>
        public void PollForFiles()
        {
            _fileList = new ArrayList();
           // _folderList = new ArrayList();

            GetFoldersAndFiles(_root);

            printArrayList(_fileList, "File list: ");
           // printArrayList(_folderList, "Folder List:");
        }

        /// <summary>
        /// Recursive helper function to traverse local directories.
        /// </summary>
        /// <param name="path">File path to traverse.</param>
        private void GetFoldersAndFiles(string path)
        {
            string[] files = null;
            string[] subDirectories = null;

            try
            {
                files = Directory.GetFiles(path);
            }
            // Throws this exception if improper file permissions
            // Writes message, continues.
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e.Message);
            }
            // Throws this exception if directory does not exist
            // Writes message, continues
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }

            if (files != null)
            {
                foreach (string file in files)
                {
                    // Add the files to the file list
                    _fileList.Add(file);
                }

                subDirectories = Directory.GetDirectories(path);
                
                foreach (string dir in subDirectories)
                {
                    // Add folder the the folder list
                    //_folderList.Add(dir);

                    // Recurse using a sub directory
                    GetFoldersAndFiles(dir);
                    
                }
            }
        }
    }
}
